package data;

import javax.media.opengl.GL;

/**
 * @author Daniel Pardo
 * This class represents a edge from one node to another
 */
public class Edge implements Drawable{
	
	private Node start;
	private Node end;
	private static Vector3 colorTree = new Vector3(0.5f,0.0f,0.0f);
	private static Vector3 colorNoneTree = new Vector3(0.0f,0.5f,0.0f);
	private static boolean drawNoneTreeEdges = true;
	public Edge(Node start, Node end)
	{
		this.start = start;
		this.end = end;
	}
	/**
	 * if one of the nodes is deleted, returns false
	 */
	@Override
	public boolean draw(GL gl) {
		if(start.isDeleted() || end.isDeleted())
			return false;
		if(start.getFather() == end || end.getFather() == start)
			gl.glColor3f(colorTree.x, colorTree.y, colorTree.z);
		else{
			if(drawNoneTreeEdges)
				gl.glColor4f(colorNoneTree.x, colorNoneTree.y, colorNoneTree.z, 0.3f);
			else
				return true;
		}
		gl.glDisable(GL.GL_TEXTURE_2D);
		gl.glPushMatrix();
		
		float xStart = start.getPosition().x;
		float yStart = start.getPosition().y;
		float xEnd = end.getPosition().x;
		float yEnd = end.getPosition().y;
		
		
		gl.glBegin(GL.GL_LINES);
			gl.glVertex2f(xStart, yStart);
			gl.glVertex2f(xEnd, yEnd);
		gl.glEnd();
		gl.glPopMatrix();
		
		gl.glEnable(GL.GL_TEXTURE_2D);
		return true;
	}

	@Override
	public boolean init(GL gl) {
		return true;
	}
	/**
	 * if set true, none tree edges are also drawn, otherwise not
	 * @param drawNoneTreeEdges
	 */
	public static void setDrawNoneTreeEdges(boolean drawNoneTreeEdges) {
		Edge.drawNoneTreeEdges = drawNoneTreeEdges;
	}

}
